Building python modules is usually fairly easy. The usual setup.py build and install commands. Its really easy to build and install to standard paths, but if we have to install to a non-standard path, then we have to build it differently.

The most ghetto way to fix this is by hacking the setup.py script by hand:

Extension(
        ...
        library_dirs=["/Volumes/DATA/alt/lib/",],
        include_dirs=["/Volumes/DATA/alt/include",],
        ...
        )

This will force distutils to pick up custom include and library directories. There is ofcourse a cleaner way to do this.

Just like ./configure, there is a way to configure in python too:

python setup.py config --include-dirs=<custom include path> --library-dirs=<custom library path>
python setup.py build

While this should build fine on most Linux machines, it might still not build properly on OSX X 10.8 or later:

ld: library not found for -lxyz
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: command 'clang' failed with exit status 1

To solve this to cleanly configure and build a module, we have to create a setup.cfg file with options for various commands:

[config]
library-dirs=/Volumes/DATA/alt/lib/
include-dirs=/Volumes/DATA/alt/include
[build_ext]
library-dirs=/Volumes/DATA/alt/lib/
include-dirs=/Volumes/DATA/alt/include

For some reason, config does not do what it is supposed to do. Secondly, the 'build' command does not have the library_dirs and include_dirs options. So the next best way is to use the alternate build commands like 'build_ext'. Once you have the setup.cfg file in place, you can do the usual and it should work.

python setup.py build_ext

TLDR

  • Create a setup.cfg with options for the build_ext command
  • Then run the setyp.py with build_ext